// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Windows.Forms; 
using System.Xml;

using QuoteAdvantageEditor.CrmSdk;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Screen for adding child products to a category
	/// </summary>
	public class ChildProductDlg : System.Windows.Forms.Form
	{
      private AuthoringEngine eng = null;
      protected XmlNode objInputNode = null;
      protected XmlNode objOutputNode = null;
     
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.Button btnCancel;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label label5;
      private System.Windows.Forms.TextBox txtProductId;
      private System.Windows.Forms.Label lblProductName;
      private System.Windows.Forms.Label lblCategoryNameLabel;
      private System.Windows.Forms.Label lblCategoryName;
      private System.Windows.Forms.RadioButton btnNewChildProduct;
      private System.Windows.Forms.RadioButton btnNoSelection;
      private System.Windows.Forms.Panel pnlNewChild;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Panel pnlNoChild;
      private System.Windows.Forms.TextBox txtDescription;
      private System.Windows.Forms.Button btnSearch;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.ComboBox ddlUnitOfMeasure;
      private System.Windows.Forms.CheckBox chkAutoAdd;
      private System.Windows.Forms.Button btnNew;
      private System.Windows.Forms.ComboBox ddlRuleFileName;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.CheckBox chkAllowDiscounting;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
      
      public AuthoringEngine Engine
      {
         get { return eng; }
         set { eng = value; }
      }

      public XmlNode InputNode
      {
         get { return objInputNode; }
         set { objInputNode = value; }
      }

      public XmlNode OutputNode
      {
         get { return objOutputNode; }
         set { objOutputNode = value; }
      }

		public ChildProductDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.btnOK = new System.Windows.Forms.Button();
         this.btnCancel = new System.Windows.Forms.Button();
         this.lblCategoryNameLabel = new System.Windows.Forms.Label();
         this.txtProductId = new System.Windows.Forms.TextBox();
         this.label2 = new System.Windows.Forms.Label();
         this.lblCategoryName = new System.Windows.Forms.Label();
         this.lblProductName = new System.Windows.Forms.Label();
         this.label5 = new System.Windows.Forms.Label();
         this.btnNewChildProduct = new System.Windows.Forms.RadioButton();
         this.btnNoSelection = new System.Windows.Forms.RadioButton();
         this.pnlNewChild = new System.Windows.Forms.Panel();
         this.btnNew = new System.Windows.Forms.Button();
         this.ddlRuleFileName = new System.Windows.Forms.ComboBox();
         this.label4 = new System.Windows.Forms.Label();
         this.ddlUnitOfMeasure = new System.Windows.Forms.ComboBox();
         this.label3 = new System.Windows.Forms.Label();
         this.btnSearch = new System.Windows.Forms.Button();
         this.chkAutoAdd = new System.Windows.Forms.CheckBox();
         this.txtDescription = new System.Windows.Forms.TextBox();
         this.label1 = new System.Windows.Forms.Label();
         this.pnlNoChild = new System.Windows.Forms.Panel();
         this.chkAllowDiscounting = new System.Windows.Forms.CheckBox();
         this.pnlNewChild.SuspendLayout();
         this.pnlNoChild.SuspendLayout();
         this.SuspendLayout();
         // 
         // btnOK
         // 
         this.btnOK.Location = new System.Drawing.Point(128, 372);
         this.btnOK.Name = "btnOK";
         this.btnOK.Size = new System.Drawing.Size(100, 23);
         this.btnOK.TabIndex = 13;
         this.btnOK.Text = "OK";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // btnCancel
         // 
         this.btnCancel.Location = new System.Drawing.Point(236, 372);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.Size = new System.Drawing.Size(100, 23);
         this.btnCancel.TabIndex = 14;
         this.btnCancel.Text = "Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // lblCategoryNameLabel
         // 
         this.lblCategoryNameLabel.Location = new System.Drawing.Point(8, 16);
         this.lblCategoryNameLabel.Name = "lblCategoryNameLabel";
         this.lblCategoryNameLabel.Size = new System.Drawing.Size(88, 16);
         this.lblCategoryNameLabel.TabIndex = 0;
         this.lblCategoryNameLabel.Text = "Category Name:";
         // 
         // txtProductId
         // 
         this.txtProductId.Location = new System.Drawing.Point(12, 24);
         this.txtProductId.Name = "txtProductId";
         this.txtProductId.Size = new System.Drawing.Size(200, 21);
         this.txtProductId.TabIndex = 5;
         this.txtProductId.Text = "";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(12, 8);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(104, 16);
         this.label2.TabIndex = 4;
         this.label2.Text = "Product ID:";
         // 
         // lblCategoryName
         // 
         this.lblCategoryName.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.lblCategoryName.Location = new System.Drawing.Point(96, 16);
         this.lblCategoryName.Name = "lblCategoryName";
         this.lblCategoryName.Size = new System.Drawing.Size(152, 16);
         this.lblCategoryName.TabIndex = 1;
         this.lblCategoryName.Text = "Name of Category";
         // 
         // lblProductName
         // 
         this.lblProductName.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.lblProductName.Location = new System.Drawing.Point(12, 72);
         this.lblProductName.Name = "lblProductName";
         this.lblProductName.Size = new System.Drawing.Size(288, 16);
         this.lblProductName.TabIndex = 11;
         this.lblProductName.Text = "Product Name";
         // 
         // label5
         // 
         this.label5.Location = new System.Drawing.Point(12, 52);
         this.label5.Name = "label5";
         this.label5.Size = new System.Drawing.Size(80, 16);
         this.label5.TabIndex = 10;
         this.label5.Text = "Product Name:";
         this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
         // 
         // btnNewChildProduct
         // 
         this.btnNewChildProduct.Location = new System.Drawing.Point(12, 40);
         this.btnNewChildProduct.Name = "btnNewChildProduct";
         this.btnNewChildProduct.Size = new System.Drawing.Size(160, 16);
         this.btnNewChildProduct.TabIndex = 2;
         this.btnNewChildProduct.Text = "Child Product";
         this.btnNewChildProduct.CheckedChanged += new System.EventHandler(this.btnNewChildProduct_CheckedChanged);
         // 
         // btnNoSelection
         // 
         this.btnNoSelection.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.btnNoSelection.Location = new System.Drawing.Point(12, 296);
         this.btnNoSelection.Name = "btnNoSelection";
         this.btnNoSelection.Size = new System.Drawing.Size(252, 16);
         this.btnNoSelection.TabIndex = 9;
         this.btnNoSelection.Text = "No Child Product Required (Empty Selection)";
         this.btnNoSelection.CheckedChanged += new System.EventHandler(this.btnNoSelection_CheckedChanged);
         // 
         // pnlNewChild
         // 
         this.pnlNewChild.Controls.Add(this.chkAllowDiscounting);
         this.pnlNewChild.Controls.Add(this.btnNew);
         this.pnlNewChild.Controls.Add(this.ddlRuleFileName);
         this.pnlNewChild.Controls.Add(this.label4);
         this.pnlNewChild.Controls.Add(this.ddlUnitOfMeasure);
         this.pnlNewChild.Controls.Add(this.label3);
         this.pnlNewChild.Controls.Add(this.btnSearch);
         this.pnlNewChild.Controls.Add(this.label2);
         this.pnlNewChild.Controls.Add(this.lblProductName);
         this.pnlNewChild.Controls.Add(this.label5);
         this.pnlNewChild.Controls.Add(this.txtProductId);
         this.pnlNewChild.Controls.Add(this.chkAutoAdd);
         this.pnlNewChild.Location = new System.Drawing.Point(32, 56);
         this.pnlNewChild.Name = "pnlNewChild";
         this.pnlNewChild.Size = new System.Drawing.Size(304, 228);
         this.pnlNewChild.TabIndex = 3;
         // 
         // btnNew
         // 
         this.btnNew.Location = new System.Drawing.Point(192, 196);
         this.btnNew.Name = "btnNew";
         this.btnNew.Size = new System.Drawing.Size(80, 23);
         this.btnNew.TabIndex = 17;
         this.btnNew.Text = "New...";
         this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
         // 
         // ddlRuleFileName
         // 
         this.ddlRuleFileName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
         this.ddlRuleFileName.Location = new System.Drawing.Point(16, 196);
         this.ddlRuleFileName.Name = "ddlRuleFileName";
         this.ddlRuleFileName.Size = new System.Drawing.Size(168, 21);
         this.ddlRuleFileName.Sorted = true;
         this.ddlRuleFileName.TabIndex = 16;
         // 
         // label4
         // 
         this.label4.Location = new System.Drawing.Point(16, 180);
         this.label4.Name = "label4";
         this.label4.Size = new System.Drawing.Size(184, 16);
         this.label4.TabIndex = 15;
         this.label4.Text = "Rule File Name for Nested Items:";
         // 
         // ddlUnitOfMeasure
         // 
         this.ddlUnitOfMeasure.DisplayMember = "Text";
         this.ddlUnitOfMeasure.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
         this.ddlUnitOfMeasure.Location = new System.Drawing.Point(104, 92);
         this.ddlUnitOfMeasure.Name = "ddlUnitOfMeasure";
         this.ddlUnitOfMeasure.Size = new System.Drawing.Size(156, 21);
         this.ddlUnitOfMeasure.TabIndex = 13;
         this.ddlUnitOfMeasure.ValueMember = "Value";
         // 
         // label3
         // 
         this.label3.Location = new System.Drawing.Point(12, 96);
         this.label3.Name = "label3";
         this.label3.Size = new System.Drawing.Size(92, 16);
         this.label3.TabIndex = 12;
         this.label3.Text = "Unit of Measure:";
         this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
         // 
         // btnSearch
         // 
         this.btnSearch.Location = new System.Drawing.Point(220, 24);
         this.btnSearch.Name = "btnSearch";
         this.btnSearch.Size = new System.Drawing.Size(80, 23);
         this.btnSearch.TabIndex = 9;
         this.btnSearch.Text = "Search...";
         this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
         // 
         // chkAutoAdd
         // 
         this.chkAutoAdd.Location = new System.Drawing.Point(16, 124);
         this.chkAutoAdd.Name = "chkAutoAdd";
         this.chkAutoAdd.Size = new System.Drawing.Size(176, 24);
         this.chkAutoAdd.TabIndex = 14;
         this.chkAutoAdd.Text = "Auto-Add Item";
         // 
         // txtDescription
         // 
         this.txtDescription.Location = new System.Drawing.Point(76, 12);
         this.txtDescription.Name = "txtDescription";
         this.txtDescription.Size = new System.Drawing.Size(204, 21);
         this.txtDescription.TabIndex = 12;
         this.txtDescription.Text = "";
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 16);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(64, 16);
         this.label1.TabIndex = 11;
         this.label1.Text = "Description:";
         this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
         // 
         // pnlNoChild
         // 
         this.pnlNoChild.Controls.Add(this.label1);
         this.pnlNoChild.Controls.Add(this.txtDescription);
         this.pnlNoChild.Location = new System.Drawing.Point(32, 312);
         this.pnlNoChild.Name = "pnlNoChild";
         this.pnlNoChild.Size = new System.Drawing.Size(284, 44);
         this.pnlNoChild.TabIndex = 10;
         // 
         // chkAllowDiscounting
         // 
         this.chkAllowDiscounting.Location = new System.Drawing.Point(16, 148);
         this.chkAllowDiscounting.Name = "chkAllowDiscounting";
         this.chkAllowDiscounting.Size = new System.Drawing.Size(176, 24);
         this.chkAllowDiscounting.TabIndex = 18;
         this.chkAllowDiscounting.Text = "Allow Discounting";
         // 
         // ChildProductDlg
         // 
         this.AcceptButton = this.btnOK;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.ClientSize = new System.Drawing.Size(346, 408);
         this.Controls.Add(this.pnlNoChild);
         this.Controls.Add(this.pnlNewChild);
         this.Controls.Add(this.btnNoSelection);
         this.Controls.Add(this.btnNewChildProduct);
         this.Controls.Add(this.lblCategoryName);
         this.Controls.Add(this.lblCategoryNameLabel);
         this.Controls.Add(this.btnCancel);
         this.Controls.Add(this.btnOK);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "ChildProductDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Child Product";
         this.Load += new System.EventHandler(this.ChildProductDlg_Load);
         this.pnlNewChild.ResumeLayout(false);
         this.pnlNoChild.ResumeLayout(false);
         this.ResumeLayout(false);

      }
		#endregion
      
      private void ChildProductDlg_Load(object sender, System.EventArgs e)
      {
         XmlNode  objCategoryNode = objInputNode.ParentNode;
         lblCategoryName.Text = objCategoryNode.Attributes["name"].Value;
         
         if (objInputNode.Name == "EmptyItem")
         {
            btnNoSelection.Checked = true;
            txtDescription.Text = objInputNode.InnerText;
            RefreshView();
            this.Show();
            txtDescription.Focus();
         }
         else
         {
            btnNewChildProduct.Checked = true;
            txtProductId.Text = objInputNode.Attributes["productId"].Value;
            lblProductName.Text = "---";

            if (txtProductId.Text != "")
            {
               LoadProduct();
            }

            RefreshView();
            RefreshAutoAdd();
            RefreshAllowDiscounting();
            RefreshUnitOfMeasure();
            RefreshRuleFileName();
            this.Show();
            txtProductId.Focus();
         }
         
      }

      private void RefreshView()
      {
         if (btnNewChildProduct.Checked)
         {
            pnlNewChild.Visible = true;
            pnlNoChild.Visible = false;
         }
         else
         {
            pnlNewChild.Visible = false;
            pnlNoChild.Visible = true;
         }
      }

      private void btnOK_Click(object sender, System.EventArgs e)
      {     
         if (btnNoSelection.Checked)
         {
            objOutputNode = objInputNode.OwnerDocument.CreateElement("", "EmptyItem", "");
            objOutputNode.InnerText = txtDescription.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
         }
         else
         {
            if (LoadProduct())
            {
               objOutputNode = objInputNode.OwnerDocument.CreateElement("", "Item", "");
               
               // Product ID attribute
               XmlAttribute objAttribute = objOutputNode.OwnerDocument.CreateAttribute("", "productId", "");
               objAttribute.Value = ""; 
               objOutputNode.Attributes.Append(objAttribute);  
               objOutputNode.Attributes["productId"].Value = txtProductId.Text;
               
               // Default Unit of Measure attribute
               objAttribute = objOutputNode.OwnerDocument.CreateAttribute("", "uom", "");
               objAttribute.Value = ""; 
               objOutputNode.Attributes.Append(objAttribute);

               string strUomValue = "";

               if (ddlUnitOfMeasure.SelectedIndex != -1)
               {
                  ListItem objItem = (ListItem)ddlUnitOfMeasure.SelectedItem;
                  strUomValue = objItem.Value;
               }

               objOutputNode.Attributes["uom"].Value = strUomValue; 
               
               // Auto-add attribute
               objAttribute = objOutputNode.OwnerDocument.CreateAttribute("", "autoAdd", "");
               objAttribute.Value = chkAutoAdd.Checked.ToString(); 
               objOutputNode.Attributes.Append(objAttribute);
               
               // Allow discounting attribute
               objAttribute = objOutputNode.OwnerDocument.CreateAttribute("", "allowDiscounting", "");
               objAttribute.Value = chkAllowDiscounting.Checked.ToString(); 
               objOutputNode.Attributes.Append(objAttribute);

               // Rule File Name
               objAttribute = objOutputNode.OwnerDocument.CreateAttribute("", "ruleFileName", "");
               objAttribute.Value = ddlRuleFileName.Text;
            
               if (objAttribute.Value == "[None]")
                  objAttribute.Value = "";

               objOutputNode.Attributes.Append(objAttribute);

               // Preserve any rules that were defined
               objOutputNode.InnerXml = objInputNode.InnerXml; 

               this.DialogResult = DialogResult.OK;
               this.Close();   
            }
         }

      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();      
      }
      
      private bool LoadProduct()
      {
         bool  bResult = false;
         Product objProduct = eng.LoadProductFromProductNumber(txtProductId.Text);

         if (objProduct == null)
         {
            lblProductName.Text = "---";
            MessageBox.Show(this, "Product ID " + txtProductId.Text + " not found.");
         }
         else
         {
            lblProductName.Text = objProduct.Name;
            bResult = true;
         }

         return bResult;
      }

      private void RefreshUnitOfMeasure()
      {
         string strDefaultUom = "";
         
         if (objInputNode.Attributes["uom"] != null)
         {
            strDefaultUom = objInputNode.Attributes["uom"].Value;               
         }
         
         Product objProduct = eng.LoadProductFromProductNumber(txtProductId.Text);

         ddlUnitOfMeasure.Items.Clear();
         
         if (objProduct != null)
         {              
            lblProductName.Text = objProduct.Name;

            ArrayList objUomList = eng.LoadUnitOfMeasureList(objProduct);

            ddlUnitOfMeasure.Items.Clear();
            ddlUnitOfMeasure.Items.Add(new ListItem("[Default]", ""));  
            ddlUnitOfMeasure.SelectedIndex = 0;

            foreach(uom objUnit in objUomList)
            {
               string strUom = objUnit.uomid.Value.ToString();
               ddlUnitOfMeasure.Items.Add(new ListItem(objUnit.name, strUom));  
                
               if (strUom == strDefaultUom)
               {
                  ddlUnitOfMeasure.SelectedIndex = ddlUnitOfMeasure.Items.Count - 1;
               }
            }
         }
      }

      private void RefreshAutoAdd()
      {
         bool bAutoAdd = false;
         
         if (objInputNode.Attributes["autoAdd"] != null)
         {
            string strValue = objInputNode.Attributes["autoAdd"].Value;
            bAutoAdd = Convert.ToBoolean(strValue);         
         }
        
         chkAutoAdd.Checked = bAutoAdd;
      }

      private void RefreshAllowDiscounting()
      {
         bool bAllowDiscounting = true;
         
         if (objInputNode.Attributes["allowDiscounting"] != null)
         {
            string strValue = objInputNode.Attributes["allowDiscounting"].Value;
            bAllowDiscounting = Convert.ToBoolean(strValue);         
         }
        
         chkAllowDiscounting.Checked = bAllowDiscounting;
      }         

      private void RefreshRuleFileName()
      {
         ddlRuleFileName.Items.Clear();

         ddlRuleFileName.Items.Add("[None]");

         XmlNodeList objItemList = objInputNode.OwnerDocument.SelectNodes("//Item");

         foreach (XmlNode node in objItemList)
         {
            XmlAttribute   attrib = node.Attributes["ruleFileName"];

            if (attrib != null)
            {                
               if (-1 == ddlRuleFileName.FindString(attrib.Value))
                  ddlRuleFileName.Items.Add(attrib.Value);
            }
         }

         string strRuleFileName = "";

         if (objInputNode.Attributes["ruleFileName"] != null)
            strRuleFileName = objInputNode.Attributes["ruleFileName"].Value;
 
         int nPos = ddlRuleFileName.FindString(strRuleFileName);

         if (nPos == -1)
         {
            nPos = ddlRuleFileName.FindString("[None]");
         }

         ddlRuleFileName.SelectedIndex = nPos;
      }

      private void btnNoSelection_CheckedChanged(object sender, System.EventArgs e)
      {
         RefreshView();
      }

      private void btnNewChildProduct_CheckedChanged(object sender, System.EventArgs e)
      {
         RefreshView();
      }  

      private void btnSearch_Click(object sender, System.EventArgs e)
      {
         LoadCrmProductDlg dlg = new LoadCrmProductDlg();

         dlg.Engine = this.Engine;

         if (DialogResult.OK == dlg.ShowDialog(this))
         {
            txtProductId.Text = dlg.SelectedProduct.ProductNumber;
            lblProductName.Text = dlg.SelectedProduct.Name;
            RefreshUnitOfMeasure();
         }
      }

      private void btnNew_Click(object sender, System.EventArgs e)
      {
         FileNameDlg dlg = new FileNameDlg();

         dlg.ShowDialog(this);

         if (dlg.DialogResult == DialogResult.OK)
         {
            int nPos = ddlRuleFileName.Items.Add(dlg.FileName);
            ddlRuleFileName.SelectedIndex = nPos;
         }
      }

	} // end class
}
